package cn.slf.ph.servlet;

import cn.slf.ph.dao.ShopDao;
import cn.slf.ph.dao.impl.ShopDaoImpl;
import cn.slf.ph.entity.Shop;
import cn.slf.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 读取action并判断
        String action = req.getParameter("action");
        ShopDao shopDao = new ShopDaoImpl();
        // 根据action进行执行不同的代码
        if ("selectAll".equals(action)){
            // 接受数据
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            // 封装到map集合
            Map<String,String> map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("shopName",shopName);
            map.put("shopPhone",shopPhone);
            // 连接数据库
            // 查询所有的数据
            List<Shop> list = shopDao.selectAll(map);
            // 查询数据总条数
            int total = shopDao.selectCount(map);
            // 给出相应信息
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("add".equals(action) || "update".equals(action)){
            String id = req.getParameter("shopId");
            int shopId = 0;
            if (id!=null && !"".equals(id)){
                shopId = Integer.parseInt(id);
            }
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopCity = req.getParameter("shopCity");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopAddress = req.getParameter("shopAddress");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopImage = req.getParameter("shopImage");
            //
            Shop shop = new Shop( shopId, shopName,  shopPhone,  shopProvince,  shopCity,  shopDistrict,  shopLatitude,  shopLongitude,  shopAddress,  shopDescribe,  shopImage);
            int num = 0;
            if ("add".equals(action)){
                num = shopDao.insert(shop);
            }else {
                num = shopDao.update(shop);
            }
            Result result = null;
            if (num!=0){
                result = Result.success("操作成功");
            }else {
                result = Result.error("操作失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("delete".equals(action)){
            String id = req.getParameter("shopId");
            int shopId = 0;
            if (id!=null && !"".equals(id)){
                shopId = Integer.parseInt(id);
            }
            int num = shopDao.delete(shopId);
            Result result = null;
            if (num!=0){
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
